home *** CD-ROM | disk | FTP | other *** search
/ L' Effet Pommier 3 / L'Effet Pommier - Volume 03.iso / Communication / I-Spy scripts / Filter and concatenate FTP.pl next >
Perl Script  |  1995-12-23  |  2KB  |  96 lines

  1. #!/usr/bin/perl
  2.  
  3. #    Filter and concatenate FTP.pl
  4. #        This script appends lines from one file to another, ignoring those lines that match a filter string
  5. #        The output is a list of FTP URLs
  6. #
  7. #        12/22/95 Version 1.0
  8. #        This script is free, public domain, no strings attached.
  9. #     Igor Livshits <igorl@uiuc.edu>
  10.  
  11.  
  12. $openToAppend=             ">> ";
  13. $macPathDelimiter=     ":";
  14. $argumentCounter=        0;
  15. $newLineDelimiter=    "\n";
  16. $omitFilterTag=            "#-" . $newLineDelimiter;
  17. $keepFilterTag=            "#+" . $newLineDelimiter;
  18. $true=                            (1 == 1);
  19. $false=                            (1 == 0);
  20.  
  21.  
  22. #    Read in the arguments passed to us from the AppleScript agent
  23. #
  24. $workingDirectory=  $ARGV[$argumentCounter++] . $macPathDelimiter;
  25. $dataDirectory=            $ARGV[$argumentCounter++] . $macPathDelimiter;
  26. $linesAddedFile=         $ARGV[$argumentCounter++];
  27. $filtersFile=                $ARGV[$argumentCounter++];
  28. $newFilesFile=            $ARGV[$argumentCounter++];
  29. $urlFile=                        $ARGV[$argumentCounter++];
  30.  
  31.  
  32. #    Read the URL
  33. #
  34. open(urlFile, $dataDirectory . $urlFile) || die "Cannot open $urlFile: $!";
  35. $rootURL= <urlFile>;
  36. if ($rootURL =~ /$newLineDelimiter$/) { chop ($rootURL); }
  37. close(urlFile);
  38.  
  39.  
  40. # Classify the filters
  41. #
  42. open(filtersFile, $dataDirectory . $filtersFile) || die "Cannot open $filtersFile: $!";
  43. $tagLine= <filtersFile>;
  44. if ($tagLine eq $keepFilterTag)
  45. {
  46.     $omitMatches= $false;                        # filter matches are kept, the rest is dropped
  47. }
  48. else                                                            # default condition
  49. {
  50.     $omitMatches= $true;                        # filter matches are dropped, the rest is kept
  51.     if ($tagLine ne $omitFilterTag)
  52.     {
  53.         seek(filtersFile, 0, 0);            # restart at the beginning of the file as the first line was not a tag
  54.     }
  55. }
  56.  
  57.  
  58. # Read the filters
  59. #
  60. @filters= ();                                            # clear the array
  61. while (<filtersFile>)
  62. {
  63.     if (/$newLineDelimiter$/) { chop; }
  64.     if ($_) { push(@filters, $_); }    # only add non-blank filters
  65. }
  66. close(filtersFile);
  67.  
  68.  
  69. #    Set up input and output files
  70. #
  71. open(linesAddedFile, $dataDirectory . $linesAddedFile) || die "Cannot open $linesAddedFile: $!";
  72. open(newFilesFile, $openToAppend . $workingDirectory . $newFilesFile) || die "Cannot open $newFilesFile: $!";
  73.  
  74.  
  75. #    Filter and add valid lines
  76. #
  77. while($aLine= <linesAddedFile>)
  78. {
  79.     $match= $false;
  80.     foreach $filter (@filters)
  81.     {
  82.         if ($aLine =~ /$filter/)
  83.         {                                                            # we have a match
  84.             $match= $true;
  85.             last;
  86.         }
  87.     }
  88.     print (newFilesFile ($rootURL . $aLine)) if ($omitMatches != $match);
  89. }
  90.  
  91.  
  92. # Clean up
  93. #
  94. close(linesAddedFile);
  95. close(newFilesFile);
  96.